if(!dojo._hasResource["market.widgets.component.Participant"]){
dojo._hasResource["market.widgets.component.Participant"]=true;
require(["dojo/_base/declare","market/widgets/CommonWidget","dojo/_base/array","dojo/query","dojo/html","dojo/_base/connect","dojo/dom-construct"],function(declare,CommonWidget,array,query,html,connect,contruct){
declare("market.widgets.component.Participant",[CommonWidget],{
_REGISTER_FORM:"market.widgets.component.registerForm",
hrefRegister:"participant/register",_currentPost:null,_currentNews:null,
showRegisterPage:function(){
   dojo.byId("divLoginForm").style.display = "none";
   dojo.byId("divRegisterForm").style.display = "inline";
   this.createForm(this._REGISTER_FORM,this.contentId,SYSTEMWIDGET.participant().hrefRegister,{},{click:this._handleClickEvents()},null,true);
},
_handleClickEvents:function(){
var clickEvents = {
   events:['btnCancel','btnAdd'],
   btnCancel:function(data){
      dojo.byId("divRegisterForm").style.display = "none";
      dojo.byId("divLoginForm").style.display = "inline";},
   btnAdd:function(data){
      SYSTEMWIDGET.genericFormWidget({widgetId:"market.widgets.component.registerForm"}).callSaveForm({
         handleSuccess:function(data){
            SYSTEMWIDGET.messageWidget().showSucc(data.messageSucc);
            dojo.byId("divRegisterForm").style.display = "none";
            dojo.byId("divLoginForm").style.display = "inline";
         }
      });}
};
return clickEvents;
},
init:function(){
   this.showDetail();
   query('#systemMenuEditProfile').onclick(function(){
      SYSTEMWIDGET.participant().showEditProfile();
   });
   query('#systemCircles').onclick(function(){
      SYSTEMWIDGET.tribeLayout().tribe();
      SYSTEMWIDGET.relationship({contentId:'center_content'}).show();
   });
   query('div.c_c_n a','rightSite').onclick(function(){
      var divParent = this.parentNode.parentNode.parentNode;
      var args = {ajaxUrl:'participant/addFriend',contentId:'rightSite',postData:'friendId='+query('input[type="hidden"]',this)[0].value,callback:function(data){
            contruct.destroy(divParent);
          }
      };
      SYSTEMWIDGET.ajaxWidget(args).callXhrPostJSON();
   });
},
showEditProfile:function(){
   SYSTEMWIDGET.configuration().show();
},
showDetail:function(){
   this._parseIntroHTML();
   this._currentNews=SYSTEMWIDGET.content();
   var _layout=SYSTEMWIDGET.tribeLayout();
   if (participant.isGuest) {
      _layout.blog();
      this._currentNews.showInboxNews();
   } else{
      _layout.privateBlog();
      this._currentNews.showInboxNews();
   }
   query("div#particicipant_menu > a").onclick(function(){
      var myType=query('input[type="hidden"]',this)[0].value;
      query("div#particicipant_menu span").removeClass("label_selected");
      query("span", this).addClass("label_selected");
      switch (myType){
      case 'myHome':
          _layout.privateBlog();
          SYSTEMWIDGET.content().showInboxNews();
          break;
      case 'myTribes':
         _layout.tribe();
         SYSTEMWIDGET.tribe().show();
         break;
      case 'myBlogs':
            _layout.blog();
            SYSTEMWIDGET.content().showPrivateNews();
         break;
      case 'myPics':
         _layout.tribe();
         SYSTEMWIDGET.album({targetNode:'center_content'}).showAlbums();
         break;
      }
   });
   var intro=participant.introduction;
   intro.content=intro.content?intro.content:"";
   if (participant.isGuest==false) {
   this._currentPost=SYSTEMWIDGET.contentEditor({targetNode:'introdution',value:intro.content,hasEdit:true,hasAttach:true,handelEvents:this._editorEvents()});
   this._currentPost._selectedFiles=intro.files;
   this._currentPost.showEditor();
   }
},
_parseIntroHTML:function(){
   var content=participant.introduction.content;
   var htmlData = '<pre>'+content?content:""+'</pre>';
   html.set(dojo.byId('introdution'),htmlData,{parseContent: true});
   this._parseFilesHTML(participant.introduction.files);
},
_parseFilesHTML:function(files){
   var htmlFiles = '';
   array.forEach(files,function(file,index){
      if (file.state!='delete'){
         htmlFiles+='<div class="box_float_left"><img src="getContent/'+file.guid+'/medium" class="box_picture1 border_thin"/>';
         htmlFiles+='<div class="removeFile" style="display:none"><input type="hidden" value="'+file.guid+'"/></div></div>';
      }
   });
   html.set(dojo.byId('introdutionFiles'),htmlFiles,{parseContent: false});
},
_initFileEvent:function(){
   var widget=this;
   query("div#introdutionFiles > div").onmouseover(function(e){
      var parentNode=e.target.parentNode;
      if (parentNode.id!='introdutionFiles'){ 
         dojo.replaceClass(parentNode,"border_fat","border_thin");
         query('> div.removeFile',parentNode).style('display','block').onclick(function(){
            widget._removeFiles(query('input[type="hidden"]', this)[0].value);
         });
      }
   });
   query("div#introdutionFiles > div").onmouseout(function(e){
      var parentNode=e.target.parentNode;
      if (parentNode.id!='introdutionFiles'){
         dojo.replaceClass(parentNode,"border_thin","border_fat");
         query('> div.removeFile',parentNode).style('display','none');
      }
   });
},
_editorEvents:function(){
   var widget=this;
   var clickEvents = {
      cancelClick:function(data){
         widget._resetIntro();
      },
      saveClick:function(data){
         var objectToQuery={'content':data};
         var index=0;
         array.forEach(widget._currentPost._selectedFiles,function(file,i){
            var state=file.state; 
            if(null==state||'new'==state){
               objectToQuery['contentFiles['+index+'].guid']=file.guid;
               index++;
            }
         });
         var introId=participant.introduction.id;
         introId=introId?introId:0;
         var args = {ajaxUrl:'saveContent/'+introId+'/introduction',postData:dojo.objectToQuery(objectToQuery),callback:function(){
            widget._currentPost.clearFiles();
            participant.introduction.files=widget._currentPost._selectedFiles;
         }};
         SYSTEMWIDGET.ajaxWidget(args).callXhrPostJSON();
         
      },
      attachClick:function(data){
         var album = SYSTEMWIDGET.album({targetNode:'center_content'}); 
         album.showAttached();
         album.handleAttached(function(){widget._addFiles();});
      },
      contentEditClick:function(data){
         widget._initFileEvent();
      }
   };
   return clickEvents;
},
_addFiles:function(){
   this._currentPost.addFiles();
   this._parseFilesHTML(this._currentPost._selectedFiles);
   this._initFileEvent();
},
_removeFiles:function(removeGUID){
   this._currentPost.removeFile(removeGUID);
   this._parseFilesHTML(this._currentPost._selectedFiles);
   this._initFileEvent();
},
_resetIntro:function(){
   this._currentPost.resetFiles();
   this._parseFilesHTML(this._currentPost._selectedFiles);
}
});});}
